package redis.spring ;

import java.util.ArrayList ;
import java.util.List ;

import org.springframework.dao.DataAccessException ;
import org.springframework.data.redis.connection.RedisConnection ;
import org.springframework.data.redis.core.RedisCallback ;
import org.springframework.data.redis.serializer.RedisSerializer ;
import org.springframework.util.Assert ;

/**
 * @author 80002165 @date 2017年6月22日 上午10:21:09
 */
public class UserDaoImpl extends AbstractBaseRedisDao<String, User> implements IUserDao {
    
    @Override
    public boolean add(User user) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                String key = user.getId() ;
                String name = user.getName() ;
                RedisSerializer<String> serializer = getRedisSerializer() ;
                byte[] keyBytes = serializer.serialize(key) ;
                byte[] nameBytes = serializer.serialize(name) ;
                return connection.setNX(keyBytes, nameBytes) ;
            }
        }) ;
        return result ;
    }
    
    @SuppressWarnings("deprecation")
    @Override
    public boolean add(List<User> list) {
        Assert.notEmpty(list) ;
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                for (User user : list) {
                    String key = user.getId() ;
                    String name = user.getName() ;
                    RedisSerializer<String> serializer = getRedisSerializer() ;
                    byte[] keyBytes = serializer.serialize(key) ;
                    byte[] nameBytes = serializer.serialize(name) ;
                    connection.setNX(keyBytes, nameBytes) ;
                }
                return true ;
            }
        }, false, true) ;
        return result ;
    }
    
    @Override
    public void delete(String key) {
        List<String> list = new ArrayList<String>() ;
        list.add(key) ;
        delete(list) ;
    }
    
    @Override
    public void delete(List<String> keys) {
        redisTemplate.delete(keys) ;
    }
    
    @Override
    public boolean update(User user) {
        User orgUser = get(user.getId()) ;
        if(orgUser == null) throw new IllegalArgumentException("数据不存在......") ;
        
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                String key = user.getId() ;
                String name = user.getName() ;
                RedisSerializer<String> serializer = getRedisSerializer() ;
                byte[] keyBytes = serializer.serialize(key) ;
                byte[] nameBytes = serializer.serialize(name) ;
                return connection.setNX(keyBytes, nameBytes) ;
            }
        }) ;
        return result ;
    }
    
    @Override
    public User get(String keyId) {
        User result = redisTemplate.execute(new RedisCallback<User>() {
            
            @Override
            public User doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer() ;
                byte[] keyBytes = serializer.serialize(keyId) ;
                byte[] names = connection.get(keyBytes) ;
                String name = serializer.deserialize(names) ;
                if(name == null) return null ;
                User user = new User() ;
                user.setId(keyId);
                user.setName(name);
                return user ;
            }
        }) ;
        return result ;
    }
    
}
